package myapp.view 
{
	import myapp.data.FlickrImage;

	import com.bumpslide.tween.FTween;
	import com.bumpslide.ui.Image;
	import com.bumpslide.ui.ThumbnailItem;

	import flash.filters.ColorMatrixFilter;

	/**
	 * Thumbnail Image Item Renderer
	 * 
	 * @author David Knape
	 */
	public class CustomThumbnailItem extends ThumbnailItem 
	{	

		private var _saturation:Number = 0;

		
		override protected function addChildren():void
		{
			super.addChildren();
			borderThickness = 1;
			borderColor = 0xaaaaaa;
			padding = 4;
			//image.debugEnabled = true;
		}

		
		/**
		 * Override the url getter to pull thumbnail url from our FlickrImage
		 */
		override public function get url():String {
			return (gridItemData==null) ? null : (gridItemData as FlickrImage).thumbnailURL;
		}

		
		//------------------------------
		// BUTTON STATES
		//-------------------------------
		override public function _off():void 
		{
			fill.color = 0xcccccc;
			FTween.ease(this, 'saturation', .3);
		}

		
		override public function _over():void 
		{
			fill.color = 0xbbbbbb;
			FTween.spring(this, 'saturation', 1);
		}
		
		
		override public function _down():void 
		{
			fill.color = 0xcccccc;
			FTween.ease(this, 'saturation', 1.5);
		}
		
		
		public function get saturation():Number {
			return _saturation;
		}
		

		public function set saturation(s:Number):void {
			_saturation = s;
			if(isNaN(s)) s = 1; 
			var r:Number = 0.213; 
			var g:Number = 0.715; 
			var b:Number = 0.072; 
			var t:Number = (1 - s);			 
			image.filters = [new ColorMatrixFilter([t*r+1-t,t*g,t*b,0,0,t*r,t*g+1-t,t*b,0,0,t*r,t*g,t*b+1-t,0,0,0,0,0,1,0])];
		}
	}
}
